//获取应用实例
const App = getApp()

// 存储所有的星星
// const stars = []


Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },


  createStar() {
    const App = getApp()
    // 存储所有的星星
    const stars = []
    const stime = 60
    let starCount = 350 //星星总的数量
    let starNum = 0 //当前生成星星数
    let deltaTime = 0
    let ctx = wx.createCanvasContext('myCanvas')
    let requestAnimationFrame = (() => {
      return (callback) => {
        setTimeout(callback, 1000 / stime)
      }
    })()
    starLoop()

    function starLoop() {
      const W = wx.getSystemInfoSync().windowWidth
      const H = wx.getSystemInfoSync().windowHeight
      requestAnimationFrame(starLoop)
      ctx.clearRect(0, 0, W, H)
      deltaTime = 11 //每次增加的星星数量
      starNum += deltaTime
      if (starNum > starCount) {
        stars.push(
          new Star(Math.random() * W, 0, Math.random() * 5 + 5)
        );
        starNum %= starCount
      }
      stars.map((s, i) => { //重复绘制
        s.update()
        s.draw()
        if (s.y >= H) { //大于屏幕高度的就从数组里去掉
          stars.splice(i, 1)
        }
      })
      ctx.draw()
    }

    function Star(x, y, radius) {
      this.x = x
      this.y = y
      this.sx = 0
      this.sy = 0
      this.deg = 0
      this.radius = radius
      this.ax = Math.random() < 0.5 ? 0.005 : -0.005
    }

    Star.prototype.update = function() {
      // 速度上限，避免速度过快
      const SPEED_LIMIT_X = 0.6
      const SPEED_LIMIT_Y = 0.7
      // 下落的加速度
      const G = 0.01

      const deltaDeg = Math.random() * 0.6 + 0.2
      this.sx += this.ax
      if (this.sx >= SPEED_LIMIT_X || this.sx <= -SPEED_LIMIT_X) {
        this.ax *= -1
      }
      if (this.sy < SPEED_LIMIT_Y) {
        this.sy += G
      }
      this.deg += deltaDeg
      this.x += this.sx
      this.y += this.sy
    }

    Star.prototype.draw = function() {
      const starImage = '../../../image/snow1.png' //星星素材
      const radius = this.radius
      ctx.save()
      ctx.translate(this.x, this.y)
      ctx.rotate(this.deg * Math.PI / 180)
      ctx.drawImage(starImage, -radius, -radius * 1.8, radius * 2, radius * 2)
      ctx.restore()
    }
  },
  setAudioPlay() {
    const adctx = wx.createInnerAudioContext()
    adctx.autoplay = true
    adctx.loop = true
    adctx.src = 'https://dn-qtshe.qbox.me/Jingle%20Bells.mp3.'
    adctx.onPlay(() => {
      console.log('开始播放')
      adctx.play()
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    if(App.globalData.isuse_CssAmt_effect == 'y'){
      this.createStar();
      this.setAudioPlay();
    }
    
    
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})